package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysDict;
import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.service.ISysDictDetailService;
import com.maoye.ossp.sys.base.service.ISysDictService;
import com.maoye.ossp.sys.base.utils.SysDictUtils;
import com.maoye.ossp.sys.site.utils.BeanToMap;

/**
 * 数据字典Contorller
 * 
 * @author lijun
 * 
 */
@Controller
@RequestMapping(value = "sys/sysdict/")
public class SysDictContorller extends BaseController {
	
	/**
	 * 是否存在 (1是 0否)
	 */
	private static final String IS_RESULT_SUCCESS = "0";
	private static final String IS_RESULT_FAIL = "1";
	
	@Resource
	private ISysDictService sysDictService;

	@Resource
	private ISysDictDetailService sysDictDetailService;

	@RequestMapping(value = "list")
	public String list(SysDict sysDict, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<SysDict> list = sysDictService.getList(BeanToMap
				.beanToMap(sysDict));
		model.addAttribute("list", list);
		model.addAttribute("sysDict", sysDict);
		return "/sys/sysdict/list";
	}

	@RequestMapping(value = "{dictId}/view")
	public String view(@PathVariable Long dictId, Model model) {
		SysDict sysDict = sysDictService.get(dictId);
		SysDictDetail sysDictDetail = new SysDictDetail();
		sysDictDetail.setDictId(dictId);
		List<SysDictDetail> list = sysDictDetailService.getList(BeanToMap.beanToMap(sysDictDetail));
		model.addAttribute("list", list);
		model.addAttribute("sysDict", sysDict);
		return "/sys/sysdict/view";
	}

	@RequestMapping(value = "{dictId}/edit")
	public String edit(@PathVariable Long dictId, Model model) {
		SysDict sysDict = sysDictService.get(dictId);
		SysDictDetail sysDictDetail = new SysDictDetail();
		sysDictDetail.setDictId(dictId);
		List<SysDictDetail> list = sysDictDetailService.getList(BeanToMap.beanToMap(sysDictDetail));
		model.addAttribute("list", list);
		model.addAttribute("sysDict", sysDict);
		return "/sys/sysdict/add";
	}

	@RequestMapping(value = "add")
	public String add(SysDict sysDict, Model model) {
		sysDict.setId(0L);
		model.addAttribute("sysDict", sysDict);
		return "/sys/sysdict/add";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public void save(@RequestParam("sysDict")String sysDictStr,@RequestParam("sysDictDetailList") String sysDictDetailListStr,
			@RequestParam("delSysDictDetailList") String delSysDictDetailListStr,RedirectAttributes redirectAttributes,HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		SysDict sysDict = gson.fromJson(sysDictStr, SysDict.class);
		List<SysDictDetail> sysDictDetailList = (List<SysDictDetail>) gson.fromJson(sysDictDetailListStr, new TypeToken<List<SysDictDetail>>(){}.getType()); 
		List<String> delSysDictDetailList = (List<String>) gson.fromJson(delSysDictDetailListStr, new TypeToken<List<String>>(){}.getType());
		sysDictService.save(sysDict,sysDictDetailList,delSysDictDetailList);
		addMessage(redirectAttributes, "保存数据成功");
		out.write("/sys/sysdict/list");
		out.flush();
		out.close();
	}

	@RequestMapping(value = "{dictId}/delete")
	public String delete(@PathVariable Long dictId, Model model,
			RedirectAttributes redirectAttributes) {
		sysDictService.remove(dictId);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/sysdict/list";
	}
	
	@RequestMapping(value = "{dictId}/{dictType}/refreshCache")
	public String refreshCache(@PathVariable Long dictId,@PathVariable String dictType, Model model,
			RedirectAttributes redirectAttributes) {
		SysDictUtils.updateCache(dictType);
		SysDict sysDict = sysDictService.get(dictId);
		SysDictDetail sysDictDetail = new SysDictDetail();
		sysDictDetail.setDictId(dictId);
		List<SysDictDetail> list = sysDictDetailService.getList(BeanToMap.beanToMap(sysDictDetail));
		model.addAttribute("list", list);
		model.addAttribute("sysDict", sysDict);
		addMessage(redirectAttributes, "刷新缓存成功！");
		return "/sys/sysdict/view";
	}
	
	/**
	 * 验证类型是否重复
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "validate")
	public void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String dictType = request.getParameter("dictType");
		String notId = request.getParameter("notId");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		SysDict sysDict = new SysDict();
		sysDict.setDictType(dictType);
		sysDict.setNotId(notId);
		List<SysDict> list = sysDictService.getList(BeanToMap.beanToMap(sysDict));
		StringBuffer result = new StringBuffer();
		if(list.size() > 0){
			result.append(IS_RESULT_FAIL);
		}else{
			result.append(IS_RESULT_SUCCESS);
		}
		out.write(result.toString());
		out.flush();
		out.close();
	}
}
